package org.ibase4j.assistant;

import java.util.Date;

import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.SchedulerMetaData;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CrawlingCronTrigger {
	public void run() throws Exception {
		// 得到调度器的引用
		SchedulerFactory sf = new StdSchedulerFactory();
		Scheduler sched = sf.getScheduler();

		// job可以在调度器运行前被调度
		// sixminutes job1 周一至周五9、10、11、14执行一次 0 30/59 9,10,11,14 ? * MON-FRI *
		// insert 5 31/59 9,10,11,13,14 ? * MON-FRI *
		JobDetail job = JobBuilder.newJob(AboutSixtyJob.class).withIdentity("sys_zhong_five", "group1").build();
		CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1").withSchedule(CronScheduleBuilder.cronSchedule("5 0/5 9,10,11,13,14,15 ? * MON-FRI *")).build();
		Date ft = sched.scheduleJob(job, trigger);

		// sixminutes job 2, 0 31/5 9,10,13,14 ? * MON-FRI * update
		// 周一至周五的上午9:01到14:59每隔3分钟更新 update
		job = JobBuilder.newJob(AboutSixtyJob.class).withIdentity("sys_zhong_five", "group2").build();
		trigger = TriggerBuilder.newTrigger().withIdentity("trigger2", "group2").withSchedule(CronScheduleBuilder.cronSchedule("15/30 1/1 9-15 ? * MON-FRI *")).build();
		ft = sched.scheduleJob(job, trigger);

		// sixminutes job 3, 0 31/5 9,10,13,14 ? * MON-FRI * update
		// 周一至周五的上午9:01到14:59每隔3分钟更新 update
		job = JobBuilder.newJob(AboutSixtyJob.class).withIdentity("sys_zhong_five", "group3").build();
		trigger = TriggerBuilder.newTrigger().withIdentity("trigger3", "group3").withSchedule(CronScheduleBuilder.cronSchedule("0 0/5 9-15 ? * MON-FRI *")).build();
		ft = sched.scheduleJob(job, trigger);
		
		// sys_zhong_fifteen
		job = JobBuilder.newJob(AboutSixtyJob.class).withIdentity("sys_zhong_fifteen", "group4").build();
		trigger = TriggerBuilder.newTrigger().withIdentity("trigger4", "group4").withSchedule(CronScheduleBuilder.cronSchedule("5 0/15 9,10,11,13,14,15 ? * MON-FRI *")).build();
		ft = sched.scheduleJob(job, trigger);

		job = JobBuilder.newJob(AboutSixtyJob.class).withIdentity("sys_zhong_fifteen", "group5").build();
		trigger = TriggerBuilder.newTrigger().withIdentity("trigger5", "group5").withSchedule(CronScheduleBuilder.cronSchedule("15/30 1/1 9-15 ? * MON-FRI *")).build();
		ft = sched.scheduleJob(job, trigger);

		job = JobBuilder.newJob(AboutSixtyJob.class).withIdentity("sys_zhong_fifteen", "group6").build();
		trigger = TriggerBuilder.newTrigger().withIdentity("trigger6", "group6").withSchedule(CronScheduleBuilder.cronSchedule("0 0/15 9-15 ? * MON-FRI *")).build();
		ft = sched.scheduleJob(job, trigger);

		// sys_zhong_thirty
		job = JobBuilder.newJob(AboutSixtyJob.class).withIdentity("sys_zhong_thirty", "group7").build();
		trigger = TriggerBuilder.newTrigger().withIdentity("trigger7", "group7").withSchedule(CronScheduleBuilder.cronSchedule("5 0/30 9,10,11,13,14,15 ? * MON-FRI *")).build();
		ft = sched.scheduleJob(job, trigger);

		job = JobBuilder.newJob(AboutSixtyJob.class).withIdentity("sys_zhong_thirty", "group8").build();
		trigger = TriggerBuilder.newTrigger().withIdentity("trigger8", "group8").withSchedule(CronScheduleBuilder.cronSchedule("15/30 0/1 9-14,15 ? * MON-FRI *")).build();
		ft = sched.scheduleJob(job, trigger);

		job = JobBuilder.newJob(AboutSixtyJob.class).withIdentity("sys_zhong_thirty", "group9").build();
		trigger = TriggerBuilder.newTrigger().withIdentity("trigger9", "group9").withSchedule(CronScheduleBuilder.cronSchedule("0 0/30 9-15 ? * MON-FRI *")).build();
		ft = sched.scheduleJob(job, trigger);

		// sys_zhong_sixty
		job = JobBuilder.newJob(AboutSixtyJob.class).withIdentity("sys_zhong_sixty", "group10").build();
		trigger = TriggerBuilder.newTrigger().withIdentity("trigger10", "group10").withSchedule(CronScheduleBuilder.cronSchedule("5 30/59 9,10,11,13,14,15 ? * MON-FRI *")).build();
		ft = sched.scheduleJob(job, trigger);

		job = JobBuilder.newJob(AboutSixtyJob.class).withIdentity("sys_zhong_sixty", "group11").build();
		trigger = TriggerBuilder.newTrigger().withIdentity("trigger11", "group11").withSchedule(CronScheduleBuilder.cronSchedule("15/30 0/1 9-15 ? * MON-FRI *")).build();
		ft = sched.scheduleJob(job, trigger);

		job = JobBuilder.newJob(AboutSixtyJob.class).withIdentity("sys_zhong_sixty", "group12").build();
		trigger = TriggerBuilder.newTrigger().withIdentity("trigger12", "group12").withSchedule(CronScheduleBuilder.cronSchedule("0 0/30 10-15 ? * MON-FRI *")).build();
		ft = sched.scheduleJob(job, trigger);

		/*
		 * // job 3 在8-17点的偶数分钟运行 //thirty job3 insert ,0 0/30 10,11,13,14 ? *
		 * MON-FRI * job =
		 * JobBuilder.newJob(InsertSixtyJob.class).withIdentity("job3",
		 * "group1").build(); trigger =
		 * TriggerBuilder.newTrigger().withIdentity("trigger3",
		 * "group1").withSchedule
		 * (CronScheduleBuilder.cronSchedule("0 0/2 8-17 * * ?")).build(); ft =
		 * sched.scheduleJob(job, trigger); log.info(job.getKey() +
		 * " has been scheduled to run at: " + ft +
		 * " and repeat based on expression: " + trigger.getCronExpression());
		 * 
		 * // job 4 在17-23点分钟是3的倍数运行 //thirty job4 update , job =
		 * JobBuilder.newJob(InsertSixtyJob.class).withIdentity("job4",
		 * "group1").build(); trigger =
		 * TriggerBuilder.newTrigger().withIdentity("trigger4",
		 * "group1").withSchedule
		 * (CronScheduleBuilder.cronSchedule("0 0/3 17-23 * * ?")).build(); ft =
		 * sched.scheduleJob(job, trigger); log.info(job.getKey() +
		 * " has been scheduled to run at: " + ft +
		 * " and repeat based on expression: " + trigger.getCronExpression());
		 * 
		 * // job 5 在每月1号或15号的10点运行 job =
		 * JobBuilder.newJob(InsertSixtyJob.class).withIdentity("job5",
		 * "group1").build(); trigger =
		 * TriggerBuilder.newTrigger().withIdentity("trigger5",
		 * "group1").withSchedule
		 * (CronScheduleBuilder.cronSchedule("0 0 10am 1,15 * ?")).build(); ft =
		 * sched.scheduleJob(job, trigger); log.info(job.getKey() +
		 * " has been scheduled to run at: " + ft +
		 * " and repeat based on expression: " + trigger.getCronExpression());
		 * 
		 * // job 6 周一至周五的每30秒运行一次 job =
		 * JobBuilder.newJob(InsertSixtyJob.class).withIdentity("job6",
		 * "group1").build(); trigger =
		 * TriggerBuilder.newTrigger().withIdentity("trigger6",
		 * "group1").withSchedule
		 * (CronScheduleBuilder.cronSchedule("0,30 * * ? * MON-FRI")) .build();
		 * ft = sched.scheduleJob(job, trigger); log.info(job.getKey() +
		 * " has been scheduled to run at: " + ft +
		 * " and repeat based on expression: " + trigger.getCronExpression());
		 * 
		 * // job 7 在周六、周日的每30秒运行一次 job =
		 * JobBuilder.newJob(InsertSixtyJob.class).withIdentity("job7",
		 * "group1").build(); trigger =
		 * TriggerBuilder.newTrigger().withIdentity("trigger7",
		 * "group1").withSchedule
		 * (CronScheduleBuilder.cronSchedule("0,30 * * ? * SAT,SUN")) .build();
		 * ft = sched.scheduleJob(job, trigger); log.info(job.getKey() +
		 * " has been scheduled to run at: " + ft +
		 * " and repeat based on expression: " + trigger.getCronExpression());
		 */

		// 调度器运行前所有任务不会运行
		sched.start();

		/*
		 * log.info("------- Started Scheduler -----------------");
		 * 
		 * log.info("------- Waiting five minutes... ------------"); try {
		 * Thread.sleep(10L * 1000L); } catch (Exception e) {
		 * e.printStackTrace(); }
		 * 
		 * log.info("------- Shutting Down ---------------------");
		 * 
		 * sched.shutdown(true);
		 * 
		 * log.info("------- Shutdown Complete -----------------");
		 */

		SchedulerMetaData metaData = sched.getMetaData();

	}

	public static void main(String[] args) throws Exception {

		CrawlingCronTrigger example = new CrawlingCronTrigger();
		example.run();
	}
}
